package com.lc.auth.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.auth.mapper.SysPostMapper;
import com.lc.common.result.Result;
import com.lc.model.system.SysPost;
import com.lc.system.annotation.Log;
import com.lc.system.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author yqr
 * @since 2023-06-12
 */
@Api(tags = "岗位管理")
@RestController
@RequestMapping("/admin/system/sysPost")
public class SysPostController {

    @Autowired
    private SysPostMapper sysPostMapper;

    @Log(title = "岗位管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    @ApiOperation(value = "列表岗位展示")
    @GetMapping("/list/{page}/{limit}")
    public Result showList(@PathVariable int page, @PathVariable int limit, SysPost sysPost){
        //分页构造器
        IPage<SysPost> pageInfo = new Page<>(page,limit);

        //条件构造器--加入条件
        LambdaQueryWrapper<SysPost> lqw = new LambdaQueryWrapper<>();
        lqw.like(sysPost.getName()!= null, SysPost::getName, sysPost.getName());
        lqw.eq(sysPost.getPostCode() !=null, SysPost::getPostCode, sysPost.getPostCode());
        lqw.eq(sysPost.getStatus()!=null, SysPost::getStatus, sysPost.getStatus());
        lqw.orderByDesc(SysPost::getUpdateTime);

        //执行分页
        sysPostMapper.selectPage(pageInfo,lqw);

        //返回分页信息
        return Result.ok(pageInfo);
    }

    //新增岗位
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysPost.add')")
    @ApiOperation(value = "新增岗位接口")
    @PostMapping("/add")
    public Result addPost(@RequestBody SysPost sysPost){
        sysPostMapper.insert(sysPost);
        return Result.ok().message("岗位新增成功!");
    }

    //编辑岗位
    @Log(title = "岗位管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @ApiOperation(value = "编辑岗位接口")
    @GetMapping("/edit/{id}")
    public Result editPost(@PathVariable String id){
        SysPost sysPost = sysPostMapper.selectById(id);
        return Result.ok(sysPost);
    }

    //修改岗位
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @ApiOperation(value = "修改岗位接口")
    @PutMapping("/update")
    public Result updatePost(@RequestBody SysPost sysPost){
        sysPostMapper.updateById(sysPost);
        return Result.ok().message("岗位修改成功!");
    }

    //删除岗位
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @ApiOperation(value = "删除岗位接口")
    @DeleteMapping("/delete/{id}")
    public Result deletePost(@PathVariable String id){
        sysPostMapper.deleteById(id);
        return Result.ok().message("岗位删除成功!");
    }
}

